package saer.send.now.operations;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import saer.send.now.channel.Channel;
import saer.send.now.channel.State;

public class GetTotalBytes implements IOperation {
    public static String        OP_CODE = "gt";

    private static final Logger log     = Logger.getLogger(GetTotalBytes.class.getName());

    @Override
    public void exec(Map<String, Channel> channelStore, String id, HttpServletRequest req, HttpServletResponse resp) {
        log.log(Level.FINEST, "Got GET_TOTAL_BYTES_SENT request, req:{0}", req);

        try {
            PrintWriter writer = resp.getWriter();
            writer.write(State.getTotalBytesSent());
            writer.close();
        } catch (IOException e) {
            log.log(Level.WARNING, "Failed to send total number of bytes sent to client, e:{1}", e);
            throw new RuntimeException("Failed to send total number of bytes sent to client", e);
        }
    }
}
